package com.ruoyi;

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.util.Base64;


import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;


public class RSACipherExample {

    public static void f1(String[] args) throws Exception {
        // 初始化密钥对生成器
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048, new SecureRandom());
        // 生成密钥对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        // 公钥
        byte[] publicKeyEncoded = keyPair.getPublic().getEncoded();
        // 私钥
        byte[] privateKeyEncoded = keyPair.getPrivate().getEncoded();

        // 加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
        byte[] data = "Hello, World!".getBytes();
        byte[] encryptedData = cipher.doFinal(data);

        // 输出
        System.out.println("Public Key: " + Base64.getEncoder().encodeToString(publicKeyEncoded));
        System.out.println("Private Key: " + Base64.getEncoder().encodeToString(privateKeyEncoded));
        System.out.println("Encrypted Data: " + Base64.getEncoder().encodeToString(encryptedData));


    }


    public static void f1() throws Exception {
        // 生成RSA密钥对
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        KeyPair pair = keyGen.generateKeyPair();
        PublicKey publicKey = pair.getPublic();
        PrivateKey privateKey = pair.getPrivate();


        // 公钥和私钥可以用于加密和解密
        // 加密
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal("Hello, World!".getBytes());

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decrypted = cipher.doFinal(encrypted);

        // 输出结果
        System.out.println("Original: " + new String(decrypted));
    }

    public static void f3(){
        try {
            // 生成RSA密钥对
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            KeyPair pair = keyGen.generateKeyPair();
            PublicKey publicKey = pair.getPublic();
            PrivateKey privateKey = pair.getPrivate();

            // 公钥和私钥的Base64编码字符串
            String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
            String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());

            System.out.println("Public Key: " + publicKeyString);
            System.out.println("Private Key: " + privateKeyString);

            // 使用公钥加密
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal("Secret Message".getBytes());
            String encryptedString = Base64.getEncoder().encodeToString(encryptedBytes);

            System.out.println("Encrypted Message: " + encryptedString);

            // 使用私钥解密
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            String decryptedString = new String(decryptedBytes);

            System.out.println("Decrypted Message: " + decryptedString);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        try{
            f3();
        }catch (Exception e){

        }
    }
}
